package edu.umassd.hciproject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

public class MyStorageManager {
	Context appContext;
	final String filename;
	boolean autoSave = true;
	JSONObject jsContent = new JSONObject(); // default as empty object

	public MyStorageManager(Context context, final String localFile) {
		appContext = context;
		filename = localFile;
		loadFromLocal();
	}

	// Get key names
	public ArrayList<String> getNames() {
		ArrayList<String> names = new ArrayList<String>();
		JSONArray jsNames = jsContent.names();
		if (jsNames != null)
			for (int i = 0; i < jsNames.length(); i++) {
				try {
					String item = jsNames.getString(i);
					names.add(item);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		return names;
	}

	public Object getContentsOf(String name) {
		Object value = null;
		try {
			value = jsContent.get(name);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return value;
	}

	// Insert new element or override existing one with same name
	public boolean putElement(String name, Object value) {
		try {
			jsContent.put(name, value);
		} catch (JSONException e) {
			return false;
		}

		if (autoSave)
			save();

		return true;
	}

	// Update element
	public boolean updateElement(String name, Object value) {
		try {
			jsContent.get(name);	// throws exception if element doesn't exist
			jsContent.put(name, value);
		} catch (JSONException e) {
			// Element doesn't exist
			e.printStackTrace();
			return false;
		}

		if (autoSave)
			save();

		return true;
	}

	public void removeElement(String name) {
		jsContent.remove(name);
		if (autoSave)
			save();
	}
	
	public void reload() {
		loadFromLocal();
	}

	public void setAutoSave(boolean enable) {
		autoSave = enable;
	}

	public boolean save() {
		return saveToLocal();
	}

	protected boolean saveToLocal() {
		// TODO Auto-generated method stub
		FileOutputStream fileStream = null;
		boolean result = false;
		try {
			fileStream = appContext.openFileOutput(filename,
					Context.MODE_PRIVATE);
			byte[] data = jsContent.toString().getBytes();
			fileStream.write(data);
			result = true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fileStream != null) {
				try {
					fileStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	protected void loadFromLocal() {
		// Load from file
		FileInputStream fileStream = null;
		String contents = new String();
		try {
			fileStream = appContext.openFileInput(filename);
			byte[] data = new byte[fileStream.available()];
			while (fileStream.read(data) >= 0) {
				contents += new String(data);
			}
			jsContent = new JSONObject(contents);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fileStream != null) {
				try {
					fileStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}
